<template>
  <div class="swiper">
    <img ref="pre" src="@/assets/pre.png" @click="toPre" />
    <div class="box" ref="swiperBox">
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
      <div class="item">
        <div class="img-box">
          <img src="@/assets/swiper1.png" alt="" />
        </div>
      </div>
    </div>
    <img ref="next" src="@/assets/next.png" @click="toNext" />
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
const swiperBox = ref();
const pre = ref();
const next = ref();
onMounted(() => {
  const top = swiperBox.value.offsetTop;
  const left = swiperBox.value.offsetLeft;
  pre.value.style.top = "36%";
  pre.value.style.left = left + 20 + "px";
  next.value.style.top = "36%";
  next.value.style.right = left + 20 + "px";
  toNext();
});
const index = ref(0);
const t = ref();
const toPre = () => {
    clearTimeout(t.value);
    if (index.value == -1) {
      index.value = -6;
      swiperBox.value.style.transition = "0s";
      setTimeout(() => {
        index.value++;
        const trans = 75 * index.value;
        swiperBox.value.style.transform = `translateX(${trans}rem)`;
        swiperBox.value.style.transition = "2s";
      });
    } else {
      swiperBox.value.style.transition = "2s";
    }
    index.value++;
    const trans = 75 * index.value;
    swiperBox.value.style.transform = `translateX(${trans}rem)`;
    t.value = setTimeout(() => {
      toNext();
    }, 5000);
  },
  toNext = () => {
    clearTimeout(t.value);
    if (index.value == -5) {
      index.value = 0;
      swiperBox.value.style.transition = "0s";
      setTimeout(() => {
        index.value--;
        const trans = 75 * index.value;
        swiperBox.value.style.transform = `translateX(${trans}rem)`;
        swiperBox.value.style.transition = "2s";
      });
    } else {
      swiperBox.value.style.transition = "2s";
    }
    index.value--;
    const trans = 75 * index.value;
    swiperBox.value.style.transform = `translateX(${trans}rem)`;
    t.value = setTimeout(() => {
      toNext();
    }, 5000);
  };
</script>
<style lang="scss" scoped>
.swiper {
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  position: relative;
  .box {
    width: 70%;
    display: flex;
    transition: 2s;
    .item {
      width: 70%;
      display: flex;
      flex-direction: column;
      align-items: center;
      flex-shrink: 0;
      position: relative;
      .img-box {
        width: 100%;
        height: 20rem;
        overflow: hidden;
        box-sizing: border-box;
        img {
          width: 100%;
          height: 22.5rem;
          //   transform: scale(1.1);
        }
      }

      .swiper-text {
        z-index: 9;
        font-size: 1rem;
        color: #0f2e4d;
        line-height: 1.375rem;
        position: absolute;
        bottom: 1.25rem;
      }
    }
  }
  img {
    position: absolute;
    width: 2.5rem;
    height: 2.5rem;
    z-index: 9;
  }
}
</style>
